//
//  BKMyOrderSplitController.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/21.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderSplitController.h"
#import "BKMyorderSplitCell.h"
#import "BKMyOrderSplitHeadView.h"
#import "BK_OD_Tool.h"
#import "BKMyOrderSplitToPayController.h"

#import "BK_OD_Color.h"


#define COLOR_08 HexColor(0xf7f7f7)//浅灰
@interface BKMyOrderSplitController ()<UITableViewDelegate,UITableViewDataSource,CellButtonClick,UIAlertViewDelegate>

@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *itemArray;
@property(nonatomic,strong)NSDictionary *dic;
@property(nonatomic,copy)NSString *order_no;
@property(nonatomic,copy)NSString *money;
@end

@implementation BKMyOrderSplitController
-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }return _itemArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"拆单";
    self.view.backgroundColor =[UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;

  
   
    _tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, ScreenHeight- 64) style:UITableViewStylePlain];
    
    BKMyOrderSplitHeadView *splitsingleHead =[[BKMyOrderSplitHeadView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 65)];
    
    _tableview.tableHeaderView = splitsingleHead;
    splitsingleHead.model = self.model;
    UIView *viewFoot =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    _tableview.tableFooterView = viewFoot;
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = viewFoot.bounds;
    button.backgroundColor =kOrderBKGreen;
    [viewFoot addSubview:button];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"拆单" forState:UIControlStateNormal];
    [self.view addSubview:_tableview];
    _tableview.backgroundColor =COLOR_08;
    self.tableview.delegate =self;
    self.tableview.dataSource = self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.model.order_list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   BKMyorderSplitCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[BKMyorderSplitCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
       
        
    }
    cell.model = self.model.order_list[indexPath.row];
        cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(void)buttonClick:(UIButton*)btn{
    
    if (self.itemArray.count == 0) {
        [HUD showMessage:@"您还没有选中商品"];
        return;
    }
    
//    [HUD showProgress:@"正在拆单"];
    [HUD showProgress:nil];
     NSDictionary *dic  =[NSDictionary dictionaryWithObjectsAndKeys:self.model.order_no,@"order_no",[self.itemArray componentsJoinedByString:@","],@"itemIds",nil];
    self.dic = dic;
    self.order_no = self.model.order_no;
    [BKMyOrderGetDataFromNetWork splitOrderGetPostageWithDic:dic Success:^(NSString *str) {
          [HUD dismiss];
        float money = [str floatValue];
        self.money = str;
        
        //支付邮费;
        if (money !=0) {
          
            UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"邮费支付" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            alter.delegate =self;
            [alter show];
        }
        
        //直接拆单
        else{
            
          [BKMyOrderGetDataFromNetWork splitOrderWithDic:dic Status:^(NSString *str) {
              if ([str isEqualToString:@"1"]) {
                  [HUD showSuccessMessage:@"拆单成功"];
                   [self.navigationController popToRootViewControllerAnimated:YES];
              }
              else{
                  [HUD showMessage:str];
              }
          }];
        }
    } failure:^(NSError *error) {
        [HUD dismiss];
        if (error.code==502) {
            [HUD showErrorMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showErrorMessage:error.localizedDescription];
        }
        
    }];
    
}
#pragma mark------------cell delegate

-(void)addItem:(NSString *)itemId{
    
    if ([self.itemArray containsObject:itemId]) {
        [self.itemArray removeObject:itemId];
    }
    else{
        [self.itemArray addObject:itemId];
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        BKMyOrderSplitToPayController *con = [BKMyOrderSplitToPayController new];
        con.order_no = self.order_no;
        con.dic = self.dic;
        con.money = self.money;
        [self.navigationController pushViewController:con animated:YES];
           }
    else {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count -3] animated:YES];
    }
}
@end
